package com.paradogs.framework.core.netty.clients;

import com.paradogs.framework.core.netty.PRAbMsgHandler;
import com.paradogs.framework.core.netty.PRConnHolder;
import com.paradogs.framework.core.netty.server.PRConnection;
import io.netty.channel.ChannelHandlerContext;

/**
 * @author: yumi
 * @date: 2023/11/3  11:18
 * @Description: TODO
 */
public abstract class PRAbClientMsgHandler<T> extends PRAbMsgHandler<T> {

    private String type;

    public PRAbClientMsgHandler(String type) {
        this.type = type;
    }

    /**
     * 连接建立事件
     * @param ctx
     * @throws Exception
     */
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        super.channelActive(ctx);
        PRConnection conn = new PRConnection();
        conn.setChannel(ctx.channel());
        PRConnHolder.addConn(type, conn);
    }

    /**
     * 连接断开事件
     * @param ctx
     * @throws Exception
     */
    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        super.channelInactive(ctx);
        PRConnHolder.removeConn(type);
    }

}
